Programming ASP.NET MVC 4 by Jess Chadwick Todd Snyder & Hrusikesh Panda
Author:Jess Chadwick, Todd Snyder & Hrusikesh Panda [Jess Chadwick, Todd Snyder, and Hrusikesh Panda]
Language: eng
Format: epub
Tags: COMPUTERS / Web / Web Programming
ISBN: 9781449320300
Publisher: O'Reilly Media
Published: 2012-09-13T16:00:00+00:00
Donut Hole Caching
Donut hole caching is the inverse of donut caching: while the donut caching technique caches the entire page, leaving out only a few small sections, donut hole caching caches only one or a few portions of the page (the donut “holes”).
For example, the Ebuy reference application contains a list of auction categories that do not change often, so it makes sense to render all of the categories just once and cache the resulting HTML.
Donut hole caching is very useful in these kinds of scenarios, where most of the elements in your page are dynamic, with the exception of a few sections that rarely change, or are changed based on a request parameter. And, unlike donut caching, ASP.NET MVC has great support for donut hole caching through the use of child actions.
Let’s see donut hole caching in action by applying it to the Ebuy auction categories example mentioned above. Here is the partial view that we will be caching:
@{ Layout = null; } <ul> @foreach(var category in ViewBag.Categories as IEnumerable<Category>) <li>@Html.ActionLink(@category.Name, "category", "categories", new { categoryId = category.Id })</li> </ul>
This partial view enumerates through all the categories and renders each one as a list item in an unordered list. Each item in the list is a link to the Category action in the Categories controller, passing the category’s Id as an action parameter.
Next, create a child action that displays this view:
[ChildActionOnly] [OutputCache(Duration=60)] public ActionResult CategoriesChildAction() { // Fetch Categories from the database and // pass it to the child view via its ViewBag ViewBag.Categories = Model.GetCategories(); return View(); }
Notice how the OutputCacheAttribute caches the result of this method for 60 seconds.
Then you can call this new action from a parent view by calling @Html.Action("CategoriesChildAction"), as shown in the following example:
<header> <h1>MVC Donut Hole Caching Demo</h1> </header> <aside> <section id="categories"> @Html.Action("CategoriesChildAction") </section> </aside> <!-- Rest of the page with non cacheable content goes here -->
Now when the page is rendered, the Categories child action is called to generate the list of categories.
The results of this call get cached via the OutputCacheAttribute, so when the page is rendered the next time, the Categories list is rendered from the cache while the rest of the page is generated from scratch.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
NET | C & C++ Windows Programming |
SQL Server | VBA |
Visual Basic |
Deep Learning with Python by François Chollet(12563)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7016)
Microservices with Go by Alexander Shuiskov(6782)
Practical Design Patterns for Java Developers by Miroslav Wengner(6693)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6635)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6043)
The Art of Crafting User Stories by The Art of Crafting User Stories(5572)
NetSuite for Consultants - Second Edition by Peter Ries(5507)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5309)
Kotlin in Action by Dmitry Jemerov(5061)
